package com.example.understandvideo.service;

import com.example.understandvideo.signer.Credentials;
import com.example.understandvideo.signer.Signer;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.LongSerializationPolicy;
import lombok.extern.slf4j.Slf4j;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;


@Slf4j
public class HttpService {
    /**
     * 泰德的host
     */
    private final String HOST;
    private final String Token;
    private final MediaType mediaType;


    private final String jsonType = "application/json";
    private final String normalType = "application/x-www-form-urlencoded";
    private final String formType = "multipart/form-data";


    private final Gson GSON;

    private final OkHttpClient HTTP_CLIENT = new OkHttpClient();

    public HttpService(String host, String token) {
        this.HOST = host;
        this.Token = token;
        this.mediaType = MediaType.parse(jsonType);
        this.GSON = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .setLongSerializationPolicy(LongSerializationPolicy.STRING)
                .create();
    }

    public void videoImage() {
        try {
            /* create credentials */
            Credentials credentials = new Credentials();
            credentials.setAccessKeyID("ak");
            credentials.setSecretAccessKey("sk");
            credentials.setRegion("cn-north-1");
            credentials.setService("cv");

            /* create signer */
            Signer signer = new Signer();

            /* create http client */
            CloseableHttpClient httpClient = HttpClients.createDefault();

            /* prepare request */
            HttpPost request = new HttpPost();
            request.setURI(new URI("https://visual.volcengineapi.com?Action=JPCartoon&Version=2020-08-26"));

            request.addHeader(HttpHeaders.USER_AGENT, "volc-sdk-java/v1.0.0");

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("image_base64", ""));
            request.setEntity(new UrlEncodedFormEntity(nvps,StandardCharsets.UTF_8));

            signer.sign(request, credentials);

            /* launch request */
            CloseableHttpResponse response = httpClient.execute(request);

            /* status code */
            System.out.println(response.getStatusLine().getStatusCode());   // 200

            /* get response body */
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String result = EntityUtils.toString(entity);
                System.out.println(result);
            }

            /* close resources */
            response.close();
            httpClient.close();
        }catch (Exception e){
            //log.error(" videoImage error {}", e.getMessage());
        }
    }
}
